<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker之Dockerfile指令 | 눈_눈？</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="雨润三江水,一任五更寒">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.39493abb.css" as="style"><link rel="preload" href="/assets/js/app.f4a44a18.js" as="script"><link rel="preload" href="/assets/js/3.48232c0c.js" as="script"><link rel="preload" href="/assets/js/1.1ffd113d.js" as="script"><link rel="preload" href="/assets/js/23.60529100.js" as="script"><link rel="prefetch" href="/assets/js/10.3ca9dec1.js"><link rel="prefetch" href="/assets/js/11.874c1055.js"><link rel="prefetch" href="/assets/js/12.ba64ad86.js"><link rel="prefetch" href="/assets/js/13.567f7e2c.js"><link rel="prefetch" href="/assets/js/14.a883120b.js"><link rel="prefetch" href="/assets/js/15.f0f48289.js"><link rel="prefetch" href="/assets/js/16.665ca6bd.js"><link rel="prefetch" href="/assets/js/17.9e5d8903.js"><link rel="prefetch" href="/assets/js/18.f72895dc.js"><link rel="prefetch" href="/assets/js/19.2a106cc2.js"><link rel="prefetch" href="/assets/js/20.4c0d7dc8.js"><link rel="prefetch" href="/assets/js/21.6771e3a2.js"><link rel="prefetch" href="/assets/js/22.f3c7aad6.js"><link rel="prefetch" href="/assets/js/24.7d50cb5e.js"><link rel="prefetch" href="/assets/js/25.58231492.js"><link rel="prefetch" href="/assets/js/26.3067a077.js"><link rel="prefetch" href="/assets/js/27.b167830e.js"><link rel="prefetch" href="/assets/js/28.f5722284.js"><link rel="prefetch" href="/assets/js/29.62d2e4fe.js"><link rel="prefetch" href="/assets/js/30.c2afa3b0.js"><link rel="prefetch" href="/assets/js/31.d7dde24d.js"><link rel="prefetch" href="/assets/js/32.4e2b272d.js"><link rel="prefetch" href="/assets/js/33.5e4d7960.js"><link rel="prefetch" href="/assets/js/34.8d5624e0.js"><link rel="prefetch" href="/assets/js/35.abd48ffb.js"><link rel="prefetch" href="/assets/js/36.e231ea32.js"><link rel="prefetch" href="/assets/js/37.e99c1b8f.js"><link rel="prefetch" href="/assets/js/38.ff825294.js"><link rel="prefetch" href="/assets/js/39.b3321103.js"><link rel="prefetch" href="/assets/js/4.936c6377.js"><link rel="prefetch" href="/assets/js/40.8e915e21.js"><link rel="prefetch" href="/assets/js/41.9a5c6a3d.js"><link rel="prefetch" href="/assets/js/42.2159b3c3.js"><link rel="prefetch" href="/assets/js/43.144bc67d.js"><link rel="prefetch" href="/assets/js/44.c7b1a0a9.js"><link rel="prefetch" href="/assets/js/45.fafbd061.js"><link rel="prefetch" href="/assets/js/46.dfd415c1.js"><link rel="prefetch" href="/assets/js/47.d6285fa6.js"><link rel="prefetch" href="/assets/js/48.c3c90153.js"><link rel="prefetch" href="/assets/js/49.fa7ab119.js"><link rel="prefetch" href="/assets/js/5.0bd232c4.js"><link rel="prefetch" href="/assets/js/50.6d5fe086.js"><link rel="prefetch" href="/assets/js/51.e0b9a5c9.js"><link rel="prefetch" href="/assets/js/52.39d71cb2.js"><link rel="prefetch" href="/assets/js/53.ad47bc0c.js"><link rel="prefetch" href="/assets/js/54.989d4596.js"><link rel="prefetch" href="/assets/js/55.fea0aaad.js"><link rel="prefetch" href="/assets/js/56.af246d88.js"><link rel="prefetch" href="/assets/js/57.747bce1d.js"><link rel="prefetch" href="/assets/js/58.8d177768.js"><link rel="prefetch" href="/assets/js/59.e97890fd.js"><link rel="prefetch" href="/assets/js/6.8aac6fc8.js"><link rel="prefetch" href="/assets/js/60.114e9ca6.js"><link rel="prefetch" href="/assets/js/7.f1ea8070.js"><link rel="prefetch" href="/assets/js/8.af678f34.js"><link rel="prefetch" href="/assets/js/9.a1b28891.js">
    <link rel="stylesheet" href="/assets/css/0.styles.39493abb.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-e3b9d544><div data-v-e3b9d544><div id="loader-wrapper" class="loading-wrapper" data-v-1c4f0192 data-v-e3b9d544 data-v-e3b9d544><div class="loader-main" data-v-1c4f0192><div data-v-1c4f0192></div><div data-v-1c4f0192></div><div data-v-1c4f0192></div><div data-v-1c4f0192></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-071f7da2 data-v-e3b9d544 data-v-e3b9d544><h3 class="title" style="display:none;" data-v-071f7da2 data-v-071f7da2>눈_눈？</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-071f7da2 data-v-071f7da2><input type="password" value="" data-v-071f7da2> <span data-v-071f7da2>Konck! Knock!</span> <button data-v-071f7da2>OK</button></label> <div class="footer" style="display:none;" data-v-071f7da2 data-v-071f7da2><span data-v-071f7da2><i class="iconfont reco-theme" data-v-071f7da2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-071f7da2>vuePress-theme-reco</a></span> <span data-v-071f7da2><i class="iconfont reco-other" data-v-071f7da2></i> <a data-v-071f7da2>yykrlc</a></span> <span data-v-071f7da2><i class="iconfont reco-copyright" data-v-071f7da2></i> <a data-v-071f7da2>2021</a></span></div></div> <div class="hide" data-v-e3b9d544><header class="navbar" data-v-e3b9d544><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/head.png" alt="눈_눈？" class="logo"> <span class="site-name">눈_눈？</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/朗读/" class="nav-link"><i class="iconfont undefined"></i>
  朗读
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="iconfont undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/Docker/" class="nav-link"><i class="iconfont undefined"></i>
  Docker
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="iconfont undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/sql/" class="nav-link"><i class="iconfont undefined"></i>
  sql
</a></li><li class="dropdown-item"><!----> <a href="/categories/其他/" class="nav-link"><i class="iconfont undefined"></i>
  其他
</a></li><li class="dropdown-item"><!----> <a href="/categories/Other/" class="nav-link"><i class="iconfont undefined"></i>
  Other
</a></li><li class="dropdown-item"><!----> <a href="/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue/" class="nav-link"><i class="iconfont undefined"></i>
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/categories/Css/" class="nav-link"><i class="iconfont undefined"></i>
  Css
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/闲言碎语/" class="nav-link"><i class="iconfont undefined"></i>
  闲言碎语
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/tools/" class="nav-link"><i class="iconfont reco-document"></i>
  收藏
</a></div><div class="nav-item"><a href="/photo/" class="nav-link"><i class="iconfont reco-douyin"></i>
  画廊
</a></div><div class="nav-item"><a href="/friends/" class="nav-link"><i class="iconfont reco-friend"></i>
  关于
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-e3b9d544></div> <aside class="sidebar" data-v-e3b9d544><div class="personal-info-wrapper" data-v-c115f482><img src="/assets/img/home-head.32578f7e.png" alt="hero" class="personal-img" data-v-c115f482> <h3 class="name" data-v-c115f482>yykrlc</h3> <div class="num" data-v-c115f482><div data-v-c115f482><h3 data-v-c115f482>44</h3> <h6 data-v-c115f482>文章</h6></div> <div data-v-c115f482><h3 data-v-c115f482>27</h3> <h6 data-v-c115f482>标签</h6></div></div> <hr data-v-c115f482></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/朗读/" class="nav-link"><i class="iconfont undefined"></i>
  朗读
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="iconfont undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/Docker/" class="nav-link"><i class="iconfont undefined"></i>
  Docker
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="iconfont undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/sql/" class="nav-link"><i class="iconfont undefined"></i>
  sql
</a></li><li class="dropdown-item"><!----> <a href="/categories/其他/" class="nav-link"><i class="iconfont undefined"></i>
  其他
</a></li><li class="dropdown-item"><!----> <a href="/categories/Other/" class="nav-link"><i class="iconfont undefined"></i>
  Other
</a></li><li class="dropdown-item"><!----> <a href="/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue/" class="nav-link"><i class="iconfont undefined"></i>
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/categories/Css/" class="nav-link"><i class="iconfont undefined"></i>
  Css
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/闲言碎语/" class="nav-link"><i class="iconfont undefined"></i>
  闲言碎语
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/tools/" class="nav-link"><i class="iconfont reco-document"></i>
  收藏
</a></div><div class="nav-item"><a href="/photo/" class="nav-link"><i class="iconfont reco-douyin"></i>
  画廊
</a></div><div class="nav-item"><a href="/friends/" class="nav-link"><i class="iconfont reco-friend"></i>
  关于
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Docker之Dockerfile指令</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/views/docker/dockerfileNote.html#dockerfile关于" class="sidebar-link">Dockerfile关于</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#dockerfile思维导图" class="sidebar-link">Dockerfile思维导图</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#dockerfile示例" class="sidebar-link">Dockerfile示例</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#dockerfile常用指令" class="sidebar-link">Dockerfile常用指令</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-1-from" class="sidebar-link">2.1 FROM</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-2-maintainer" class="sidebar-link">2.2 MAINTAINER</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-3-run" class="sidebar-link">2.3 RUN</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-4-cmd" class="sidebar-link">2.4 CMD</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-5-expose" class="sidebar-link">2.5 EXPOSE</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-6-env" class="sidebar-link">2.6 ENV</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-7-add" class="sidebar-link">2.7 ADD</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-8-copy" class="sidebar-link">2.8 COPY</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-9-entrypoint" class="sidebar-link">2.9 ENTRYPOINT</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-10-volume" class="sidebar-link">2.10 VOLUME</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-11-user" class="sidebar-link">2.11 USER</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-12-workdir" class="sidebar-link">2.12 WORKDIR</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-13-healthcheck" class="sidebar-link">2.13 HEALTHCHECK</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/views/docker/dockerfileNote.html#_2-14-arg" class="sidebar-link">2.14 ARG</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/views/docker/dockerfileNote.html#参考" class="sidebar-link">参考</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-071f7da2 data-v-e3b9d544><h3 class="title" style="display:none;" data-v-071f7da2 data-v-071f7da2>Docker之Dockerfile指令</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-071f7da2 data-v-071f7da2><input type="password" value="" data-v-071f7da2> <span data-v-071f7da2>Konck! Knock!</span> <button data-v-071f7da2>OK</button></label> <div class="footer" style="display:none;" data-v-071f7da2 data-v-071f7da2><span data-v-071f7da2><i class="iconfont reco-theme" data-v-071f7da2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-071f7da2>vuePress-theme-reco</a></span> <span data-v-071f7da2><i class="iconfont reco-other" data-v-071f7da2></i> <a data-v-071f7da2>yykrlc</a></span> <span data-v-071f7da2><i class="iconfont reco-copyright" data-v-071f7da2></i> <a data-v-071f7da2>2021</a></span></div></div> <div data-v-e3b9d544><main class="page"><!----> <div class="page-title" style="display:none;"><h1>Docker之Dockerfile指令</h1> <hr> <div data-v-3cf1d7fb><i class="iconfont reco-account" data-v-3cf1d7fb><span data-v-3cf1d7fb>yykrlc</span></i> <i class="iconfont reco-date" data-v-3cf1d7fb><span data-v-3cf1d7fb>2020-04-02</span></i> <i class="iconfont reco-eye" data-v-3cf1d7fb><span id="/views/docker/dockerfileNote.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-3cf1d7fb><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-3cf1d7fb><span class="tag-item" data-v-3cf1d7fb>
      docker
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="dockerfile关于"><a href="#dockerfile关于" class="header-anchor">#</a> Dockerfile关于</h2> <p>在<code>Docker</code>中创建镜像最常用的方式，就是使用<code>Dockerfile</code>。<code>Dockerfile</code>是一个<code>Docker</code>镜像的描述文件，<code>Dockerfile</code>包含了一条条的指令，每一条指令构建一层，因此每一条指令的内容，就是描述该层应当如何构建。</p> <h2 id="dockerfile思维导图"><a href="#dockerfile思维导图" class="header-anchor">#</a> <code>Dockerfile</code>思维导图</h2> <p><img src="https://upload-images.jianshu.io/upload_images/15668934-8fc9d137e3a31f9d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p> <h2 id="dockerfile示例"><a href="#dockerfile示例" class="header-anchor">#</a> <code>Dockerfile</code>示例</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># FROM指定所需依赖的基础镜像 ,格式：FROM &lt;image&gt;:&lt;tag&gt;</span>
FROM java:8

<span class="token comment"># 持久化到指定目录</span>
VOLUME /tmp

<span class="token comment"># ADD复制文件，格式：ADD &lt;src&gt; &lt;dest&gt;</span>
ADD eureka-server-1.0-SNAPSHOT.jar  app.jar

<span class="token comment"># RUN在容器构建过程中执行的命令,格式：RUN &lt;command&gt;</span>
RUN <span class="token function">bash</span>  -c <span class="token string">&quot;touch /app.jar&quot;</span>

<span class="token comment"># EXPOSE声明需要对外暴露的端口</span>
EXPOSE  <span class="token number">8761</span>

<span class="token comment">#ENTRYPOINT  指定docker容器启动时执行的命令</span>
<span class="token comment">##&quot;-Djava.security.egd=file:/dev/./urandom&quot; 加快随机数产生过程</span>
ENTRYPOINT <span class="token punctuation">[</span><span class="token string">&quot;java&quot;</span>,<span class="token string">&quot;-Djava.security.egd=file:/dev/./urandom&quot;</span>,<span class="token string">&quot;-jar&quot;</span>,<span class="token string">&quot;/app.jar&quot;</span><span class="token punctuation">]</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="dockerfile常用指令"><a href="#dockerfile常用指令" class="header-anchor">#</a> <code>Dockerfile</code>常用指令</h2> <h2 id="_2-1-from"><a href="#_2-1-from" class="header-anchor">#</a> 2.1 FROM</h2> <p>指明构建的新镜像是来自于哪个基础镜像，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>FROM centos:6
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-2-maintainer"><a href="#_2-2-maintainer" class="header-anchor">#</a> 2.2 MAINTAINER</h2> <p>指明镜像维护者及其联系方式（一般是邮箱地址），例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>MAINTAINER DuebassLei <span class="token operator">&lt;</span><span class="token number">1130122701</span>@qq.com<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>不过，<code>MAINTAINER</code>并不推荐使用，更推荐使用LABEL来指定镜像作者，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>LABEL <span class="token assign-left variable">maintainer</span><span class="token operator">=</span><span class="token string">&quot;DuebassLei&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-3-run"><a href="#_2-3-run" class="header-anchor">#</a> 2.3 RUN</h2> <p>构建镜像时运行的<code>Shell</code>命令，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>RUN <span class="token punctuation">[</span><span class="token string">&quot;yum&quot;</span>, <span class="token string">&quot;install&quot;</span>, <span class="token string">&quot;httpd&quot;</span><span class="token punctuation">]</span>
RUN yum <span class="token function">install</span> httpd
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_2-4-cmd"><a href="#_2-4-cmd" class="header-anchor">#</a> 2.4 CMD</h2> <p>启动容器时执行的<code>Shell</code>命令，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>CMD <span class="token punctuation">[</span><span class="token string">&quot;-C&quot;</span>, <span class="token string">&quot;/start.sh&quot;</span><span class="token punctuation">]</span> 
CMD <span class="token punctuation">[</span><span class="token string">&quot;/usr/sbin/sshd&quot;</span>, <span class="token string">&quot;-D&quot;</span><span class="token punctuation">]</span> 
CMD /usr/sbin/sshd -D
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="_2-5-expose"><a href="#_2-5-expose" class="header-anchor">#</a> 2.5 EXPOSE</h2> <p>声明容器运行的服务端口，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>EXPOSE <span class="token number">80</span> <span class="token number">443</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-6-env"><a href="#_2-6-env" class="header-anchor">#</a> 2.6 ENV</h2> <p>设置环境内环境变量，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ENV MYSQL_ROOT_PASSWORD <span class="token number">123456</span>
ENV JAVA_HOME /usr/local/jdk1.8.0_45
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_2-7-add"><a href="#_2-7-add" class="header-anchor">#</a> 2.7 ADD</h2> <p>拷贝文件或目录到镜像中，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ADD <span class="token operator">&lt;</span>src<span class="token operator">&gt;</span><span class="token punctuation">..</span>.<span class="token operator">&lt;</span>dest<span class="token operator">&gt;</span>
ADD html.tar.gz /var/www/html
ADD https://xxx.com/html.tar.gz /var/www/html
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>***PS：***如果是URL或压缩包，会自动下载或自动解压。</p> <h2 id="_2-8-copy"><a href="#_2-8-copy" class="header-anchor">#</a> 2.8 COPY</h2> <p>拷贝文件或目录到镜像中，用法同ADD，只是不支持自动下载和解压，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>COPY ./start.sh /start.sh
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-9-entrypoint"><a href="#_2-9-entrypoint" class="header-anchor">#</a> 2.9 ENTRYPOINT</h2> <p>启动容器时执行的<code>Shell</code>命令，同<code>CMD</code>类似，只是由<code>ENTRYPOINT</code>启动的程序<strong>不会被docker run命令行指定的参数所覆盖</strong>，而且，<strong>这些命令行参数会被当作参数传递给ENTRYPOINT指定指定的程序</strong>，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>ENTRYPOINT <span class="token punctuation">[</span><span class="token string">&quot;/bin/bash&quot;</span>, <span class="token string">&quot;-C&quot;</span>, <span class="token string">&quot;/start.sh&quot;</span><span class="token punctuation">]</span>
ENTRYPOINT /bin/bash -C <span class="token string">'/start.sh'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>***PS：***<code>Dockerfile</code>文件中也可以存在多个<code>ENTRYPOINT</code>指令，但仅有最后一个会生效。</p> <h2 id="_2-10-volume"><a href="#_2-10-volume" class="header-anchor">#</a> 2.10 VOLUME</h2> <p>指定容器挂载点到宿主机自动生成的目录或其他容器，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>VOLUME <span class="token punctuation">[</span><span class="token string">&quot;/var/lib/mysql&quot;</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>***PS：***一般不会在Dockerfile中用到，更常见的还是在docker run的时候指定-v数据卷。</p> <h2 id="_2-11-user"><a href="#_2-11-user" class="header-anchor">#</a> 2.11 USER</h2> <p>为RUN、CMD和ENTRYPOINT执行Shell命令指定运行用户，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token environment constant">USER</span> <span class="token operator">&lt;</span>user<span class="token operator">&gt;</span><span class="token punctuation">[</span>:<span class="token operator">&lt;</span>usergroup<span class="token operator">&gt;</span><span class="token punctuation">]</span>
<span class="token environment constant">USER</span> <span class="token operator">&lt;</span><span class="token environment constant">UID</span><span class="token operator">&gt;</span><span class="token punctuation">[</span>:<span class="token operator">&lt;</span><span class="token environment constant">UID</span><span class="token operator">&gt;</span><span class="token punctuation">]</span>
<span class="token environment constant">USER</span> edisonzhou
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="_2-12-workdir"><a href="#_2-12-workdir" class="header-anchor">#</a> 2.12 WORKDIR</h2> <p>为RUN、CMD、ENTRYPOINT以及COPY和AND设置工作目录，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>WORKDIR /data
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-13-healthcheck"><a href="#_2-13-healthcheck" class="header-anchor">#</a> 2.13 HEALTHCHECK</h2> <p>告诉Docker如何测试容器以检查它是否仍在工作，即健康检查，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>HEALTHCHECK --interval<span class="token operator">=</span>5m --timeout<span class="token operator">=</span>3s --retries<span class="token operator">=</span><span class="token number">3</span> <span class="token punctuation">\</span>
    CMD <span class="token function">curl</span> -f http:/localhost/ <span class="token operator">||</span> <span class="token builtin class-name">exit</span> <span class="token number">1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><blockquote><p>一些选项的说明：</p></blockquote> <ul><li><code>--interval=DURATION (default: 30s)</code>：每隔多长时间探测一次，默认30秒</li> <li><code>-- timeout= DURATION (default: 30s)</code>：服务响应超时时长，默认30秒</li> <li><code>--start-period= DURATION (default: 0s)</code>：服务启动多久后开始探测，默认0秒</li> <li><code>--retries=N (default: 3)</code>：认为检测失败几次为宕机，默认3次</li></ul> <blockquote><p>一些返回值的说明：</p></blockquote> <ul><li><code>0</code>：容器成功是健康的，随时可以使用</li> <li><code>1</code>：不健康的容器无法正常工作</li> <li><code>2</code>：保留不使用此退出代码</li></ul> <h2 id="_2-14-arg"><a href="#_2-14-arg" class="header-anchor">#</a> 2.14 ARG</h2> <p>在构建镜像时，指定一些参数，例如：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>FROM centos:6
ARG user <span class="token comment"># ARG user=root</span>
<span class="token environment constant">USER</span> <span class="token variable">$user</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这时，我们在docker build时可以带上自定义参数user了，如下所示：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>docker build --build-arg <span class="token assign-left variable">user</span><span class="token operator">=</span>DuebassLei Dockerfile <span class="token builtin class-name">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="参考"><a href="#参考" class="header-anchor">#</a> <em>参考</em></h3> <p><a href="https://www.cnblogs.com/edisonchou/p/dockerfile_inside_introduction.html" target="_blank" rel="noopener noreferrer">你必须知道的Dockerfile<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p><a href="https://docs.docker.com/engine/reference/builder/" target="_blank" rel="noopener noreferrer">Dockerfile reference<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">6/29/2020, 3:45:05 PM</span></div></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-e3b9d544><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;background-color:rgba(231, 234, 241,.5);display:none;" data-v-44bd5a18 data-v-44bd5a18><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-44bd5a18><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-44bd5a18></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-44bd5a18></path></svg></div><div data-v-932dc3c2></div></div></div>
    <script src="/assets/js/app.f4a44a18.js" defer></script><script src="/assets/js/3.48232c0c.js" defer></script><script src="/assets/js/1.1ffd113d.js" defer></script><script src="/assets/js/23.60529100.js" defer></script>
  </body>
</html>
